package com.mcy.springbatch.job;

import com.mcy.springbatch.job.BaseJobConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * flow的使用，多个step的集合，即可以被多个Job复用
 */
@Slf4j
@Configuration
public class CFlowJobConfig extends BaseJobConfig {

    @Bean
    public Job flowDemoJob() {
        return jobBuilderFactory.get("flowDemoJob")
                .incrementer(new RunIdIncrementer())
                .start(flowDemoFlow())
                .next(flowDemoStep3())
                .end()
                .build();
    }

    /**
     * 创建Flow，指定包含的step
     *
     * @return
     */
    @Bean
    public Flow flowDemoFlow() {
        return new FlowBuilder<Flow>("flowDemoFlow")
                .start(flowDemoStep1())
                .next(flowDemoStep2())
                .build();
    }

    @Bean
    public Step flowDemoStep1() {
        return stepBuilderFactory.get("flowDemoStep1")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("flowDemoStep1...");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    @Bean
    public Step flowDemoStep2() {
        return stepBuilderFactory.get("flowDemoStep2")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("flowDemoStep2...");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    @Bean
    public Step flowDemoStep3() {
        return stepBuilderFactory.get("flowDemoStep3")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("flowDemoStep3...");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

}
